package cn.stylefeng.guns.modular.rms.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.rms.entity.Company;
import cn.stylefeng.guns.modular.rms.entity.Course;
import cn.stylefeng.guns.modular.rms.entity.CourseApply;
import cn.stylefeng.guns.modular.rms.mapper.CourseApplyMapper;
import cn.stylefeng.guns.modular.rms.mapper.CourseMapper;
import cn.stylefeng.guns.modular.rms.model.params.CourseApplyParam;
import cn.stylefeng.guns.modular.rms.model.params.CourseParam;
import cn.stylefeng.guns.modular.rms.model.result.CourseApplyResult;
import  cn.stylefeng.guns.modular.rms.service.CourseApplyService;
import cn.stylefeng.guns.modular.ums.model.params.MemberParam;
import cn.stylefeng.guns.utils.MemberTokenUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 课程报名 服务实现类
 * </p>
 *
 * @author yzb
 * @since 2022-04-28
 */
@Service
public class CourseApplyServiceImpl extends ServiceImpl<CourseApplyMapper, CourseApply> implements CourseApplyService {
    @Resource
    private CourseMapper courseMapper;

    @Transactional
    @Override
    public void add(CourseApplyParam param){
        //判断是否存在，如果不存在，则新建
        CourseApplyResult result=this.findBySpec(param);
        if (result!=null){
            throw new RuntimeException("已报名，无需重复报名");
        }
        CourseApply entity = getEntity(param);
        this.save(entity);
        //添加报名人数
        Course course=courseMapper.selectById(param.getCourseId());
        course.setNumber(course.getNumber()+1);
        courseMapper.updateById(course);
    }

    @Override
    public void delete(CourseApplyParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CourseApplyParam param){
        CourseApply oldEntity = getOldEntity(param);
        CourseApply newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CourseApplyResult findBySpec(CourseApplyParam param){
        return baseMapper.findBySpec(param);
    }

    @Override
    public List<CourseApplyResult> findListBySpec(CourseApplyParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CourseApplyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<CourseApplyResult> list=page.getRecords();
        for (int i=0;i<list.size();i++){
            list.get(i).setCourse(courseMapper.findBySpec(new CourseParam().setId(list.get(i).getCourseId())));
        }
        page.setRecords(list);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public LayuiPageInfo findPageBySpecForAdmin(CourseApplyParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageListForAdmin(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(CourseApplyParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private CourseApply getOldEntity(CourseApplyParam param) {
        return this.getById(getKey(param));
    }

    private CourseApply getEntity(CourseApplyParam param) {
        CourseApply entity = new CourseApply();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
